package com.geticsen.leetcode;

import java.util.*;
//https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
public class Solution_17 {
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits == null || digits.length() < 1) return result;
        Map<String, List<String>> numberMap = new HashMap<>();
        numberMap.put("2", Arrays.asList("a", "b", "c"));
        numberMap.put("3", Arrays.asList("d", "e", "f"));
        numberMap.put("4", Arrays.asList("g", "h", "i"));
        numberMap.put("5", Arrays.asList("j", "k", "l"));
        numberMap.put("6", Arrays.asList("m", "n", "o"));
        numberMap.put("7", Arrays.asList("p", "q", "r", "s"));
        numberMap.put("8", Arrays.asList("t", "u", "v"));
        numberMap.put("9", Arrays.asList("w", "x", "y", "z"));
        letterCombinations(digits, 0, result, numberMap, "");
        return result;
    }

    public static void letterCombinations(String digits, int index, List<String> result, Map<String, List<String>> numberMap, String res) {
        if (res.length() == digits.length()) {
            result.add(res);
            return;
        }
        List<String> strings = numberMap.get(String.valueOf(digits.charAt(index)));
        for (String str : strings) {
            letterCombinations(digits, index + 1, result, numberMap, res + str);
        }
    }
}
